import router from './router'
import {verify} from '@/api/user'

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => { //路由跳转前触发
  
  if(to.path === '/login'){
    next()
  }else{
    //校验令牌
    var token = localStorage.getItem("token")
    if(!token){
      next(`/login?redirect=${to.path}`)
    }else{
      //异步校验令牌是否合法
      verify().then(res=>{
        if(res.success){
          next();
        }else{
          next(`/login?redirect=${to.path}`)
        }
      })
    }
  }
})

router.afterEach(() => {  //路由跳转后触发
 
})
